(function(window) {
	var vm, pullRefresh;
	var devSN = "";
	var userName = "";
	var commonLocale = dataManager.getLocale();

	function pulldownRefresh() {
		v3Restshop.gerShop(11, 3421, 2, function(code, data) {
			console.log(JSON.stringify(data));
			if(data.code === 0) {
				vm.total = data.data.allCount;
				vm.online = data.data.onLineCount;
				vm.offline = data.data.offLineCount;
				data.data.list.forEach(function(item) {
					if(item.devList.length !== 0) {
						item.devList.forEach(function(n) {
							n.status = n.status == 1 ? '在线' : '离线';
						})
					}
				})
				vm.list = data.data.list;
			}
		})
	}

	function pullupRefresh() {
		v3Restshop.gerShop(11, 3421, 2, function(code, data) {
			if(data.code === 0) {
				vm.total = data.data.allCount;
				vm.online = data.data.onLineCount;
				vm.offline = data.data.offLineCount;
				data.data.list.forEach(function(item) {
					if(item.devList.length !== 0) {
						item.devList.forEach(function(n) {
							n.status = n.status == 1 ? '在线' : '离线';
						})
					}
				})
				vm.list = vm.list.concat(data.data.list);
				pullRefresh.endPullUpToRefresh(true);
			}
		})
	}

	function pullRefresh() {
		pulldownRefresh();
		pullRefresh.endPullDownToRefresh();
	}

	function onlineList() {
		v3Restshop.gerShop(11, 3421, 1, function(code, data) {
			console.log(JSON.stringify(data));
			if(data.code === 0) {
				data.data.list.forEach(function(item) {
					//					item.status = item.status == 1 ? '在线' : '离线'; 
				})
				vm.list = data.data.list;
			}
		})
	}

	function offlineList() {
		v3Restshop.gerShop(11, 3421, 0, function(code, data) {
			console.log(JSON.stringify(data));
			if(data.code === 0) {
				data.data.list.forEach(function(item) {
					//					item.status = item.status == 1 ? '在线' : '离线'; 
				})
				vm.list = data.data.list;
			}
		})
	}

	window.addEventListener('addDeviceToShop', function(event) {
		if(regionNameOfClickedShop && vm.shopInfo[regionNameOfClickedShop]) {
			$.each(vm.shopInfo[regionNameOfClickedShop], function(i, shop) {
				if(shop.shopId === event.detail.shopId) {
					shop.devSN = event.detail.devSN;
					shop.shopStatus = 3;
					shop.shopHealth = vm.$t('shopStatus[2]');

					//获取场所健康度
					var shopIds = [shop.shopId];
					getShopHealth(shopIds);
					return false;
				}
			});
		}
	});

	window.addEventListener("addNewShop", function(event) {
		vm.shopCount++;
		if(!vm.shopInfo[event.detail.regionName]) {
			vm.$set(vm.shopInfo, event.detail.regionName, []);
		}
		vm.shopInfo[event.detail.regionName].push({
			shopName: event.detail.shopName,
			shopId: event.detail.shopId,
			scenarioName: event.detail.scenarioName,
			scenarioId: event.detail.scenarioId,
			devSN: event.detail.devSN,
			shopHealth: vm.$t('shopStatus[0]'), //"无设备",
			shopStatus: 0,
			customType: event.detail.customType //to do 添加新场所时需要发送这个参数
		});
	});

	function mounted() {
		vm = this;
		pullRefresh = $('#pullrefresh').pullToRefresh({
			down: {
				callback: pullRefresh
			},
			up: {
				contentrefresh: '正在加载···',
				contentnomore: '没有更多数据了',
				callback: pullupRefresh
			}
		});
		$.plusReady(function() {
			devSN = $.util.getDevSN();
			userName = dataManager.getUserName();
			pulldownRefresh();
		});
	}

	window.device = {
		mounted: mounted,
		data: {
			total: 0,
			online: 0,
			offline: 0,
			list: []
		},
		methods: {
			onlineList: onlineList,
			offlineList: offlineList,
			allList: pulldownRefresh,
			deviceDetail: function(value) {
				$.util.setDevSN(value.devSn);
				$.util.setShopId(value.shopId);
				$.util.setShopInfo(value);
				if(value.customType === 'xiaoxiaobei') {
					commonTool.openWindow('../../dist/new/device/smallBieIndex.html', 'smallBieIndex.html', {
						value: value
					});
				} else {
					commonTool.openWindow('../../dist/new/device/smallBieIndex.html', 'smallBieIndex.html', {
					//commonTool.openWindow('../../dist/new/device/bigAcIndex.html', 'bigAcIndex.html', {
						value: value
					});
				}
			}
		}
	};
})(window);